var prefix = ctx + "warn/warnings"
var number = 0;
var busiData;
$(function() {
	test();
	load();
	//如果没有数据存在则，自定义展示添加，删除 提示
	$(".no-records-found").remove();
	
	$(".type-select").chosen({
		width : '90%',
		allow_single_deselect : true,
		search_contains : true
	});
	
	// 删除行
	$("body").on("click", ".btn-warning", function() {
		if($("#exampleTable2 tbody").find("tr").size() <= 1){
			layer.msg("只有一行数据，无法删除");
			return;
		}
		console.log($(this).parents("tr"));
		$(this).parents("tr").remove();
	});
	// 添加行
	$("body").on("click", ".btn-primary", function() {
		$(this).parents("tr").after(addTr());
		tableSelectLoad();
		$(".type-select").chosen({
			width : '90%',
			allow_single_deselect : true,
			search_contains : true
		});
	});
	
	// input[type=number] 限制 负数，小数 输入
	$("body").on('click',"input[type=number]",function(){
        if(! /^\d+$/.test(this.value)){
            this.value='';
        }
	});
	$("body").on('keyup',"input[type=number]",function(){
        if(! /^\d+$/.test(this.value)){
            this.value='';
        }
	});
	
	tableSelectLoad();
	
});

function load() {
	$('#exampleTable2').bootstrapTable({
		ajaxOptions : {
			async : false
		},
		method : 'get', // 服务器数据的请求方式 get or post
		url : prefix + "/listBusi", // 服务器数据的加载地址
		iconSize : 'outline',
		toolbar : '#exampleToolbar',
		striped : true, // 设置为true会有隔行变色效果
		dataType : "json", // 服务器返回的数据类型
		sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者
		queryParams : function(params) {
			return {
				warnId : $("#warnId").val()
			};
		},
		columns : [
			{
				title : '表名',
				formatter : function(value, row, index) {
					number++
					var strVar = "<select name=\"busiId\" data-placeholder=\"选择指标\"";
						strVar +=	"		class=\"form-control table-select chosen-select table-select-"+ index +"\">";
						strVar +=	"	<option value=\"\">选择指标</option>";
						
					for (var i = 0; i < busiData.length; i++) {
						strVar += "<option " + ((row.busiId == busiData[i].busiId)?"selected=selected":"") + " data-busi-id=\""+ busiData[i].busiId +"\" data-busi-name=\""+ ((busiData[i].busiName == null || busiData[i].busiName == "")?"":busiData[i].busiName) +"\" value=\""+ busiData[i].busiId +"\">"
								 + busiData[i].busiName +  "</option>";
					}
						strVar +=	"</select>";
					return strVar;
				}
			},{
				title : '类型',
				formatter : function(value, row, index) {
					var strVar = "";
					    strVar += "<select data-placeholder=\"请选择数据类型\" name=\"comparator\" id=\"select-0\" class=\"chosen-select form-control type-select \" style=\"display: none;\">\n";
					    strVar += "	<option value=\"\">--请选择--<\/option>\n";
					    strVar += "	<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&gt;\""+ ((row.comparator == ">")?"selected=selected":"") +">大于<\/option>\n";
					    strVar += "	<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&gt;=\""+ ((row.comparator == ">=")?"selected=selected":"") +">大于等于<\/option>\n";
					    strVar += "	<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"=\""+ ((row.comparator == "=")?"selected=selected":"") +">等于<\/option>\n";
					    strVar += "	<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&lt;\""+ ((row.comparator == "<")?"selected=selected":"") +">小于<\/option>\n";
					    strVar += "	<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&lt;=\""+ ((row.comparator == "<=")?"selected=selected":"") +">小于等于<\/option>\n";
					    strVar += "<\/select>\n";
					return strVar;
				}
			},{
				title : '数量',
				formatter : function(value, row, index) {
					var strVar = "";
						strVar += "<input id=\"number\" name=\"number\" class=\"form-control\" type=\"number\" placeholder=\"数量\"  value=\""+ row.number +"\">";
					return strVar;
				}
			},{
				title : '操作',
				formatter : function(value, row, index) {
					var strVar = "";
						strVar += "		<a class=\"btn btn-primary btn-sm \" href=\"#\" mce_href=\"#\" title=\"添加\"><i class=\"fa fa-plus\"><\/i><\/a>\n";
					    strVar += "		<a class=\"btn btn-warning btn-sm \" href=\"#\" title=\"删除\" mce_href=\"#\"><i class=\"fa fa-remove\"><\/i><\/a>\n";
					return strVar;
				}
			}
		],
		onLoadSuccess : function() {
			$(".chosen-select").chosen('destroy').chosen({
				maxHeight : 200,
				search_contains: true
			});
			$(".type-select").chosen('destroy').chosen({
				width : '90%',
				allow_single_deselect : true,
				search_contains : true
			});
		},
		formatNoMatches:function(){
			var strVar = "";
		    strVar += "<tr data-index=\"0\">\n";
		    strVar += "	<td style=\"\">\n";
		    strVar += "		<select name=\"busiId\" data-placeholder=\"选择指标\" class=\"form-control chosen-select table-select-"+ number +"\">\n";
		    strVar += "			<option value=\"\">选择指标<\/option>\n";
		    strVar += "		<\/select>\n";
		    strVar += "	<\/td>\n";
		    strVar += "	<td style=\"\">\n";
		    strVar += "		<select data-placeholder=\"请选择数据类型\" name=\"comparator\" id=\"select-0\" class=\"chosen-select form-control type-select \" style=\"display: none;\">\n";
		    strVar += "			<option value=\"\">--请选择--<\/option>\n";
		    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&gt;\" selected=\"selected\">大于<\/option>\n";
		    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&gt;=\">大于等于<\/option>\n";
		    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"=\">等于<\/option>\n";
		    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&lt;\">小于<\/option>\n";
		    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&lt;=\">小于等于<\/option>\n";
		    strVar += "		<\/select>\n";
		    strVar += "	<\/td>\n";
		    strVar += "	<td style=\"\">\n";
		    strVar += "		<input id=\"number\" name=\"number\" class=\"form-control\" type=\"number\" placeholder=\"数量\">\n";
		    strVar += "	<\/td>\n";
		    strVar += "	<td style=\"\">\n";
		    strVar += "		<a class=\"btn btn-primary btn-sm \" href=\"#\" mce_href=\"#\" title=\"添加\"><i class=\"fa fa-plus\"><\/i><\/a>\n";
		    strVar += "		<a class=\"btn btn-warning btn-sm \" href=\"#\" title=\"删除\" mce_href=\"#\"><i class=\"fa fa-remove\"><\/i><\/a>\n";
		    strVar += "	<\/td>\n";
		    strVar += "<\/tr>\n";
		return strVar;
		//return "你的自定义文字说明";
	    }
	});
}

function addTr() {
	number++;
	    
	var strVar = "";
	    strVar += "<tr data-index=\"0\">\n";
	    strVar += "	<td style=\"\">\n";
	    strVar += "		<select name=\"busiId\" data-placeholder=\"选择指标\" class=\"form-control chosen-select table-select-"+ number +"\">\n";
	    strVar += "			<option value=\"\">选择指标<\/option>\n";
	    strVar += "		<\/select>\n";
	    strVar += "	<\/td>\n";
	    strVar += "	<td style=\"\">\n";
	    strVar += "		<select data-placeholder=\"请选择数据类型\" name=\"comparator\" id=\"select-0\" class=\"chosen-select form-control type-select \" style=\"display: none;\">\n";
	    strVar += "			<option value=\"\">--请选择--<\/option>\n";
	    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&gt;\" selected=\"selected\">大于<\/option>\n";
	    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&gt;=\">大于等于<\/option>\n";
	    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"=\">等于<\/option>\n";
	    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&lt;\">小于<\/option>\n";
	    strVar += "			<option data-column-length=\"\" data-column-precision=\"\" data-column-scale=\"\" value=\"&lt;=\">小于等于<\/option>\n";
	    strVar += "		<\/select>\n";
	    strVar += "	<\/td>\n";
	    strVar += "	<td style=\"\">\n";
	    strVar += "		<input id=\"number\" name=\"number\" class=\"form-control\" type=\"number\" placeholder=\"数量\">\n";
	    strVar += "	<\/td>\n";
	    strVar += "	<td style=\"\">\n";
	    strVar += "		<a class=\"btn btn-primary btn-sm \" href=\"#\" mce_href=\"#\" title=\"添加\"><i class=\"fa fa-plus\"><\/i><\/a>\n";
	    strVar += "		<a class=\"btn btn-warning btn-sm \" href=\"#\" title=\"删除\" mce_href=\"#\"><i class=\"fa fa-remove\"><\/i><\/a>\n";
	    strVar += "	<\/td>\n";
	    strVar += "<\/tr>\n";
	return strVar;
}


function test(){
	
	var html = "<option value=\"\">选择指标</option>";
	$.ajax({
		url : '/md/business/list',
		async : false,
		data : {
			algorithmType : 2, // 单表统计
			dataType : 1 //法人
		},
		success : function(data) {
			// 加载数据
			busiData = data.rows;
		}
	});
}

//表下拉框
function tableSelectLoad() {
	var html = "<option value=\"\">选择指标</option>";
	
	for (var i = 0; i < busiData.length; i++) {
		html += '<option data-busi-id="'+ busiData[i].busiId +'" data-busi-name="'+ ((busiData[i].busiName == null || busiData[i].busiName == "")?"":busiData[i].busiName) +'" value="'+ busiData[i].busiId +'">'
				 + busiData[i].busiName +  '</option>';
	}
	$(".table-select-"+number).html(html);
	$(".table-select-"+number).chosen('destroy').chosen({
		maxHeight : 200,
		search_contains: true
	});
	// 点击事件
	$(".table-select-"+number).off('change').on('change', function(e, params) {
		//console.log('second:'+params.selected);
		let select = $(this).find("option:selected");
		
		$("#warnTable").val(params.selected);
		
		$("#warnTableId").val(select.data('tableId'));
		
	});
}

/**
 * 添加预警指标
 * @returns
 */
function batchColumnsAdd() {
	var rows = new Array();
	var obj;
	var source = $('#exampleTable2 tbody tr');
	var columnsData = $('#exampleTable2').bootstrapTable('getOptions').data;
	var warnId = $("#warnId").val();
	for (let i = 0, j = 1; i < $('#exampleTable2 tbody tr').length; i++, j++) {
		if($(source[i]).hasClass("no-records-found")){
			continue;
		}
		obj = new Object();
		obj.warnId = warnId;
		obj.busiId = $(source[i]).find('select[name=busiId]').val();
		obj.comparator = $(source[i]).find('select[name=comparator]').val();
		obj.number = $(source[i]).find('input[name=number]').val();
		rows.push(obj);
	}

	console.log(JSON.stringify(rows));
	layer.confirm("确认要添加选中的'" + rows.length + "'个指标吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var json = new Array();
		// 遍历所有选择的行数据，取每条数据对应的ID
		$.each(rows, function(i, row) {
			delete row["0"];
			json.push(row);
		});
		console.log(JSON.stringify(json));
		$.ajax({
			type : "POST",
			contentType : 'application/json;charset=utf-8', // 设置请求头信息
			dataType : "json",
			data : JSON.stringify(json),
			url : prefix + '/batchBusiAdd/' + $("#warnId").val(),
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
					parent.layer.close(index);
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {
	});
}


